
function initButtonEvents() {
    $("#next").click(function(){
        var regattaAllow = $('input[name=drive]:checked').val() == 'sailing';
        $("#summary_type_purpose").text(
            $('input[name=purpose]:checked').val()
        );
        $("#summary_type_drive").text(
            $('input[name=drive]:checked').val()
        );
        $("#summary_type_hull").text(
            $('input[name=hull]:checked').val()
        );
        $("#summary_owner").text(
            $('#owner option:selected').text().trim() == "" ?
                "Unknown" : $('#owner option:selected').text()
        );
        $("#summary_yacht_name").text(
            $('#yacht_name').val()
        );
        $("#summary_description").text(
            $('#description').val()
        );
        if(regattaAllow) {
            var regattas = "";
            var regatta_ids = "";
            $("input[type=checkbox]:checked").each(function(){
                regattas += $("#regatta_text_" + $(this).val()).text() + "<br>";
                regatta_ids += $(this).val() + ";";
            });
            $("#regatta_ids").val(regatta_ids);
            $("#summary_regattas").html(regattas);
            $("#regattas_title").text("");
        } else {
            $("#regattas_title").text(" (*Regattas allows only for Sailing drive.)");
            $("input[type=checkbox]").attr("checked", false);
            $("input[type=checkbox]").attr("disabled", true);
            $("#summary_regattas").html("<i>No regattas</i>");
            $("#regatta_ids").val("");
        }

        previewImage();
    });
    $("#back").click(function(){
        var regattaAllow = $('input[name=drive]:checked').val() == 'sailing';
        if(!regattaAllow) {
            $("input[type=checkbox]").attr("disabled", true);
        }
    });
}



function initWizard() {
    $(function () {
        $("#demoForm").formwizard({
                formPluginEnabled:true,
                validationEnabled:true,
                focusFirstInput:true,
                formOptions:{
                    beforeSubmit : function(){
                        $('#back').hide();
                        $('#next').hide();
                        $("#result").text("Please wait...");
                        $('#result').css('color', "green").css('text-align', 'center');
                    },
                    success:function (responseData) {
                        // responseData contains JSON string. dataType: 'text'
                        // multipart data + POST + jQuery cause problem for Opera Browser
                        // TODO: solve problem
                        var data = responseData ;
                        $('#back').hide();
                        $('#next').hide();

                        $('#result').html("New yacht successfully added ("
                                + "<a href='/yacht/" + data.yachtId + "'>Go to yacht page</a>)"
                                + "<br><b>You will be redirected to the home page in 3 seconds...</b>");
                        $('#result').css('color', "green").css('text-align', 'center');
                        setTimeout( function(){
                                window.location = "/home";
                            }
                            , 3000);
                    },
                    error:function(data, status) {
                        $('#back').show();
                        $('#next').show();
                        $('#result').html('Something went wrong =(');
                        $('#result').css('color', "red").css('text-align', 'center');
                    },
                    // responseData contains JSON string. dataType: 'text'
                    // multipart data + POST + jQuery cause problem for Opera Browser
                    // TODO: solve problem
                    dataType: 'json',
                    resetForm:false
                },
                validationOptions : {
                    rules: { photo: { required: false, accept: "png|jpe?g|gif"  }},
                    messages: { photo: "File must be JPG, GIF or PNG" }
                }
            }
        );
    });
}

var inputPhoto;

function changePhoto(input) {
    inputPhoto = input;
    $("#demoForm").valid();
}

function previewImage() {
    if(!$('#photo').val()) {
        $('#upload-photo').hide();
        return;
    }
    if (inputPhoto.files && inputPhoto.files[0]) {
        $('#upload-photo').show();
        var reader = new FileReader();

        reader.onload = function (e) {
            $('#upload-photo').attr('src', e.target.result);
        }

        reader.readAsDataURL(inputPhoto.files[0]);
    }
}

$(document).ready(function(){
    initWizard();
    initButtonEvents();
});
